<?php
namespace app\user\model\user;

use app\common\model\BaseModel;
use app\user\logic\user\Account as User;

/**
 * 关注模型
 */
class Visitor extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'user_visit';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'update_time'      => 'integer',
        'user_id'          => 'integer',
        'visit_user_id'    => 'integer',
    ];

    /**
     * 用户查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-24
     */
    public function scopeMid($query, $mid = 0)
    {
        $mid = $mid ? : request()->mid;
        $query->where('user_id', $mid);
    }

    /**
     * 访客查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-24
     */
    public function scopeVisitor($query, $user_id = 0)
    {
        $user_id = $user_id ? : request()->mid;
        $query->where('visit_user_id', $user_id);
    }

    /**
     * 最近查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-24
     */
    public function scopeRecent($query)
    {
        $query->where('update_time', '>', time()-86400*7);    
    }

    // 关联访问者用户头像
    public function visitorInfo()
    {
        return $this->belongsTo(User::class)->field('id,nick_name,signature,avatar')->append(['avatar_url']);
    }


}
